/* _____________________________________________________________________________

	Project: The Effects of Independent Local Radio on Tanzanian Public Opinion: Evidence from a Planned Natural Experiment
	Authors: Donald P. Green, Dylan Groves, Constantine Manda, Beatrice Montano, Bardia Rahmani
	
	Purpose: Difference in Difference Analysis
	Date: 2022.10
________________________________________________________________________________*/

/* Introduction ________________________________________________________________*/
	
	clear all	
	set more off
	
/* Set Control Directory _______________________________________________________*/

	*global user "X:/Dropbox/Wellspring Tanzania Papers/Wellspring Tanzania - Natural Experiment/pfm_ne_replication"
		
/* Load Data ___________________________________________________________________*/	
	
	use "${user}/pfm_ne_replication_data.dta", clear
	sort id_resp_uid
	set seed 1956

/* Define Globals and Locals ___________________________________________________*/

	gen b_em_reject = b_em_reject_story


	#d ;
	
		/* Set seed */
		set seed 			1956
							;
								
		/* Indices */			
		global DVARIABLES 
							ipv_rej_disobey 
							ipv_norm_rej 
							em_reject
							fm_reject 
							;	

				
		/* Covariates */	
		global cov_always	i.block_ne
							svy_replacement
							;	
						
		/* Lasso Covariates */
		global cov_lasso	
							resp_age resp_female resp_muslim b_resp_standard7 b_resp_married ///
							b_resp_religiosity b_resp_literate b_resp_lang_swahili ///
							b_resp_numhh b_resp_numkid b_resp_numolder b_resp_numyounger b_resp_numadult ///
							b_ge_index b_ge_raisekids b_ge_earning b_ge_school b_ge_leadership b_ge_noprefboy ///
							b_em_reject_story b_fm_reject ///
							b_ipv_rej_index b_ipv_norm_rej b_ipv_wifereact ///
							b_radio_any b_radio_community_dum b_radio_news_dum ///
							b_values_likechange b_values_money b_values_techgood ///
							b_values_elders b_values_respectauthority b_values_individualism ///
							b_radio_programs_music b_radio_programs_sport b_radio_programs_news b_radio_programs_romance b_radio_programs_social b_radio_programs_relig ///
							b_s3q2_freetime_rest b_s3q2_freetime_bar b_s3q2_freetime_sportwatch b_s3q2_freetime_sportplay ///
							b_s3q2_freetime_tv b_s3q2_freetime_cell b_s3q2_freetime_radio b_s3q2_freetime_community ///
							b_s3q3_converse_elec b_s3q3_converse_ipv b_s3q3_converse_discr b_s3q3_converse_vote b_s3q3_converse_water b_s3q3_converse_teachers ///
							b_asset_cell b_asset_tv b_asset_chair b_asset_sofa b_asset_table b_asset_motorcycle b_asset_radio_num ///
							b_freetime_media b_discuss_ptix b_discuss_nothing b_radio_news b_dk_ptix ///
							b_collact b_contactgov ///
							v_cell v_cell_bar v_timetotown ///
							v_electricity v_muslim v_mosques ///
							v_churches v_totworship v_villexec v_pop v_subvills ///
							v_poplist v_poplist_final ///
							enum1 enum2 enum3 enum4 enum5 enum6 enum7 ///
							enum8 enum9 enum10 enum11 enum12 enum13 enum14 enum15 enum16 enum17 enum18 enum19 enum20 
							;
							
		global cov_lasso_replacement 							
							resp_muslim resp_female resp_age ///
							v_cell v_cell_bar v_timetotown ///
							v_electricity v_muslim v_mosques ///
							v_churches v_totworship v_villexec v_pop v_subvills ///
							v_poplist v_poplist_final  ///
							enum1 enum2 enum3 enum4 enum5 enum6 enum7 enum8 ///
							enum9 enum10 enum11 enum12 enum13 enum14 enum15 ///
							enum16 enum17 enum18 enum19 enum20
							;
	#d cr

	
/* Create Interaction Variables ________________________________________________*/

	foreach var of global cov_lasso {
		qui gen `var'_int = `var' * svy_nonreplacement
	}

	foreach var of global cov_lasso_replacement {
		qui gen `var'_int_replace = `var' * svy_replacement
	}
	
	foreach var of global cov_lasso {
		qui egen std_`var' = std(`var')
		qui replace `var' = std_`var'
		qui drop std_`var'
	}

	foreach var of global cov_lasso_replacement {
		qui egen std_`var' = std(`var')
		qui replace `var' = std_`var'
		qui drop std_`var'
	}
	

/* Set Excel ___________________________________________________________________*/
	
	sort id_resp_uid
	set seed 1956

	
	/* Set Put Excel File Name */
		putexcel clear
		putexcel set "${user}/pfm_ne_replication_rawoutput_diffINdiff.xlsx", sheet(ALL, replace) modify
		
		qui putexcel A1 = ("variable")
		qui putexcel B1 = ("variablelabel")
		qui putexcel C1 = ("coef")
		qui putexcel D1 = ("se")
		qui putexcel E1 = ("pval")
		qui putexcel F1 = ("r2")
		qui putexcel G1 = ("N")
		qui putexcel H1 = ("boot_p")
		
		qui putexcel I1 = ("l_coef")
		qui putexcel K1 = ("l_se")
		qui putexcel L1 = ("l_pval")
		qui putexcel M1 = ("l_r2")
		qui putexcel N1 = ("l_N")
		qui putexcel O1 = ("l_boot_p")
		
		qui putexcel P1 = ("c_coef")
		qui putexcel Q1 = ("c_se")
		qui putexcel R1 = ("c_boot_p")
		qui putexcel S1 = ("c_r2")
		qui putexcel T1 = ("c_N")
		qui putexcel U1 = ("c_ctls")
		qui putexcel V1 = ("c_ctls_num")
		
		qui putexcel W1 = ("d_coef")
		qui putexcel X1 = ("d_se")
		qui putexcel Y1 = ("d_boot_p")
		qui putexcel Z1 = ("d_r2")
		qui putexcel AA1 = ("d_N")
		qui putexcel AB1 = ("d_ctls")
		qui putexcel AC1 = ("d_ctls_num")
		qui putexcel AD1 = ("l_lasso_ctls_num")
		
	local row = 2

	
/* Run Regresions ______________________________________________________________*/

	foreach dv of global DVARIABLES {

	sort id_resp_uid
	set seed 1956

	/* Drop Macros */
	macro drop lasso_ctls 
	macro drop lasso_ctls_num 
	macro drop lasso_ctls_int
	
	macro drop lasso_ctls_replacement
	macro drop lasso_ctls_num_replacement 
	macro drop lasso_ctls_int_replace

	
		/* Variable name */	
		di "`dv'"	
		ds `dv'
			global variable = "`r(varlist)'"  
			
		/* Variable label */
		global variablelabel : var label `dv'
		
		
		** NONE
		reg `dv' treat ${cov_always}, cluster(id_village_uid)
			matrix table = r(table)
		
			sort id_resp_uid
			boottest treat, p(upper) nograph seed(1956)
				global boot_p = r(p)		
		
			/* Save values from regression */
			global coef 	= table[1,1]    	//beta
			global se 		= table[2,1]		//se
			global t 		= table[3,1]		//t
			global pval 	= table[4,1]		//pval
			global lowb		= table[5,1]		//95 conf int lower bound
			global uppb		= table[6,1]		//95 conf int upper bound
			global r2 		= `e(r2_a)' 		//r-squared
			global N 		= e(N) 				//N
			
			
		** LASSO
			
		/* Run and save lasso for non-replacements */
		sort id_resp_uid
		set seed 1956
		qui lasso linear `dv' ${cov_lasso} if svy_replacement == 0, rseed(1956)
			global lasso_ctls = e(allvars_sel)
			global lasso_ctls_num = e(k_nonzero_sel)
			
		/* Run and save lasso for replacements */
		sort id_resp_uid
		set seed 1956
		qui lasso linear `dv' ${cov_lasso_replacement} if svy_replacement == 1, rseed(1956)
			global lasso_ctls_replacement = e(allvars_sel)
			global lasso_ctls_num_replacement 	= e(k_nonzero_sel)
		
		/* Create interaction term globals */
		if ${lasso_ctls_num} != 0 {							
			foreach ctl_var of global lasso_ctls {
				global lasso_ctls_int ${lasso_ctls_int} `ctl_var'_int
			}
		}
		
		/* Create interaction terms globals for replacement */
		if ${lasso_ctls_num_replacement} != 0 {
			foreach ctl_var of global lasso_ctls_replacement {
				global lasso_ctls_int ${lasso_ctls_int_replace} `ctl_var'_int_replace
			}
		}
		
				/* If lasso selected covariates for both */
				if ${lasso_ctls_num} != 0 & ${lasso_ctls_num_replacement} != 0 {
					reg `dv' treat 	${lasso_ctls} ${lasso_ctls_int} ///
										${svy_ctls_replace} ${lasso_ctls_int_replace} svy_replacement ///
										${cov_always}, cluster(id_village_uid)
					matrix table = r(table)
					
					sort id_resp_uid
					boottest treat, p(upper) nograph seed(1956)
						global l_boot_p = r(p)
						
					di "LASSO SELECTED COVARIATES FOR BOTH"
				}
				
				/* If lasso selected covariates for only non-replacements */
				if ${lasso_ctls_num} != 0 & ${lasso_ctls_num_replacement} == 0 {		// If no lassovars chosen
					reg `dv' treat 	${lasso_ctls} ${lasso_ctls_int} ///
										svy_replacement ///
										${cov_always}, cluster(id_village_uid)
					matrix table = r(table)
					
					sort id_resp_uid
					boottest treat, p(upper) nograph seed(1956)
						global l_boot_p = r(p)	
						
					di "LASSO SELECTED COVARIATES NON-REPLACEMENTS"
				}
				
				/* If lasso selected covariates for only replacements */
				if ${lasso_ctls_num} == 0 & ${lasso_ctls_num_replacement} != 0 {		// If no lassovars chosen
					reg `dv' treat 	${svy_ctls_replace} ${lasso_ctls_int_replace} svy_replacement ///
										${cov_always}, cluster(id_village_uid)
					matrix table = r(table)
					
					sort id_resp_uid
					boottest treat , nograph seed(1956)
						global l_boot_p = r(p)
						
					di "LASSO SELECTED COVARIATES ONLY REPLACEMENTS"
				}
				
				/* If lasso selected no covariates */
				if ${lasso_ctls_num} == 0 & ${lasso_ctls_num_replacement} == 0 {		// If no lassovars chosen
					reg `dv' treat 	svy_replacement ///
										${cov_always}, cluster(id_village_uid)
					matrix table = r(table)
					
					sort id_resp_uid
					boottest treat , nograph seed(1956)
						global l_boot_p = r(p)
						
					di "NO LASSO SELECTED COVARIATES"
				}
				
			/* Save values from regression */
			global l_coef 	= table[1,1]    	//beta
			global l_se 	= table[2,1]		//se
			global l_t 		= table[3,1]		//t
			global l_pval 	= table[4,1]		//pval
			global l_lowb	= table[5,1]		//95 conf int lower bound
			global l_uppb	= table[6,1]		//95 conf int upper bound
			global l_r2 	= `e(r2_a)' 		//r-squared
			global l_N 		= e(N) 				//N
			

		** CONTROL FOR BASELINE

		sort id_resp_uid
		set seed 1956

		reg `dv' treat ${cov_always} b_`dv', cluster(id_village_uid)
			matrix table = r(table)
		
			sort id_resp_uid
			boottest treat, p(upper) nograph seed(1956)
				global c_boot_p = r(p)			// bootstrapped pval

			/* Save values from regression */
			global c_coef 	= table[1,1]    	//beta
			global c_se 	= table[2,1]		//se
			global c_t 		= table[3,1]		//t
			global c_lowb	= table[5,1]		//95 conf int lower bound
			global c_uppb	= table[6,1]		//95 conf int upper bound
			global c_r2 	= `e(r2_a)' 		//r-squared
			global c_N 		= e(N) 				//N
		
		
		** SUBTRACT BASELINE
		gen d_`dv' = `dv' - b_`dv'
		reg d_`dv' treat ${cov_always}, cluster(id_village_uid)
			matrix table = r(table) 
			
			sort id_resp_uid
			boottest treat, p(upper) nograph seed(1956)
				global d_boot_p = r(p)				// bootstrapped pval
				
			/* Save values from regression */
			global d_coef 		= table[1,1]    	//beta
			global d_se 		= table[2,1]		//se
			global d_t 			= table[3,1]		//t
			global d_lowb		= table[5,1]		//95 conf int lower bound
			global d_uppb		= table[6,1]		//95 conf int upper bound
			global d_r2 		= `e(r2_a)' 		//r-squared
			global d_N 			= e(N) 				//N
			
			
		** Export
		qui putexcel A`row' = ("${variable}")
		qui putexcel B`row' = ("${variablelabel}")
		qui putexcel C`row' = ("${coef}")
		qui putexcel D`row' = ("${se}")
		qui putexcel E`row' = ("${pval}")
		qui putexcel F`row' = ("${r2}")
		qui putexcel G`row' = ("${N}")
		qui putexcel H`row' = ("${boot_p}")
		
		qui putexcel I`row' = ("${l_coef}")
		qui putexcel K`row' = ("${l_se}")
		qui putexcel L`row' = ("${l_pval}")
		qui putexcel M`row' = ("${l_r2}")
		qui putexcel N`row' = ("${l_N}")
		qui putexcel O`row' = ("${l_boot_p}")
		
		qui putexcel P`row' = ("${c_coef}")
		qui putexcel Q`row' = ("${c_se}")
		qui putexcel R`row' = ("${c_boot_p}")
		qui putexcel S`row' = ("${c_r2}")
		qui putexcel T`row' = ("${c_N}")
		qui putexcel U`row' = ("${c_ctls}")
		qui putexcel V`row' = ("${c_ctls_num}")
		
		qui putexcel W`row' = ("${d_coef}")
		qui putexcel X`row' = ("${d_se}")
		qui putexcel Y`row' = ("${d_boot_p}")
		qui putexcel Z`row' = ("${d_r2}")
		qui putexcel AA`row' = ("${d_N}")
		qui putexcel AB`row' = ("${d_ctls}")
		qui putexcel AC`row' = ("${d_ctls_num}")
		qui putexcel AD`row' = ("${lasso_ctls_num}")

		
	/* Update locals ___________________________________________________________*/
		
		local row = `row' + 1
	}
